package com.itmck.handler;

import com.itmck.dto.response.ApiResultResponse;
import com.itmck.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 太阳当空照,花儿对我笑
 * <p>
 * Create by M ChangKe 2021/11/3 10:33
 **/
@Slf4j
@RestControllerAdvice
public class ApiExceptionHandler {

    @ExceptionHandler(value = NullPointerException.class)
    public ApiResultResponse<String> responseException(NullPointerException exception) {
        log.error("空指针异常", exception);
        return ApiResultResponse.error("空指针异常", "500");
    }

    @ExceptionHandler(value = Exception.class)
    public ApiResultResponse<String> defaultHandler(Exception exception) {
        log.error("系统异常", exception);
        return ApiResultResponse.error("空指针异常", "500");
    }

    @ExceptionHandler(value = BusinessException.class)
    public ApiResultResponse<String> businessHandler(BusinessException exception) {
        log.error("业务异常", exception);
        return ApiResultResponse.error(exception.getMessage(), exception.getCode());
    }
}